// 获取游戏容器和坦克元素
const gameContainer = document.getElementById('game-container');
const tank = document.getElementById('tank');

// 坦克的初始位置和移动速度
let tankX = gameContainer.offsetWidth / 2 - tank.offsetWidth / 2;
let tankY = gameContainer.offsetHeight - tank.offsetHeight;
let tankSpeed = 5;

// 更新坦克的位置
function updateTankPosition() {
    tank.style.left = tankX + 'px';
    tank.style.bottom = tankY + 'px';
}

// 处理键盘事件
document.addEventListener('keydown', function (event) {
    switch (event.key) {
        case 'ArrowLeft':
            if (tankX > 0) {
                tankX -= tankSpeed;
            }
            break;
        case 'ArrowRight':
            if (tankX < gameContainer.offsetWidth - tank.offsetWidth) {
                tankX += tankSpeed;
            }
            break;
        case ' ':
            // 发射子弹
            const bullet = document.createElement('div');
            bullet.classList.add('bullet');
            bullet.style.left = tankX + tank.offsetWidth / 2 - bullet.offsetWidth / 2 + 'px';
            bullet.style.bottom = tankY + tank.offsetHeight + 'px';
            gameContainer.appendChild(bullet);

            // 子弹移动
            const bulletSpeed = 10;
            const bulletInterval = setInterval(function () {
                const bulletY = parseInt(bullet.style.bottom);
                if (bulletY < gameContainer.offsetHeight) {
                    bullet.style.bottom = bulletY + bulletSpeed + 'px';
                } else {
                    clearInterval(bulletInterval);
                    gameContainer.removeChild(bullet);
                }
            }, 20);
            break;
    }
    updateTankPosition();
});

// 初始化坦克位置
updateTankPosition();